package com.peanut.baby.mvp.livedetail.detail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.expert.ExpertProfileActivity;
import com.peanut.baby.mvp.liveroom.LiveRoomActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class LiveDetailFragment extends BaseMvpFragment implements View.OnClickListener, LiveDetailFragmentContract.View{

    private static final String TAG = LiveDetailFragment.class.getSimpleName();

    @BindView(R.id.enter_room)
    TextView enterRoom;
    @BindView(R.id.expert_container)
    LinearLayout expertContainer;
    @BindView(R.id.live_detail_title)
    TextView roomTitle;
    @BindView(R.id.live_detail_price)
    TextView price;
    @BindView(R.id.live_detail_time)
    TextView time;
    @BindView(R.id.live_detail_enroled)
    TextView enroled;
    @BindView(R.id.live_detail_allow)
    TextView allow;
    @BindView(R.id.live_detail_summary)
    TextView summary;
    @BindView(R.id.live_detail_expert_avatar)
    CircleImageView expertAvatar;
    @BindView(R.id.live_detail_expert_name_title)
    TextView expertNameTitle;
    @BindView(R.id.live_detail_expert_hospital)
    TextView expertHospital;
    @BindView(R.id.live_detail_expert_summary)
    TextView expertSummary;

    Unbinder unbinder;
    int liveId;
    LiveRoom liveRoom;

    private LiveDetailFragmentContract.Presenter presenter;

    public static LiveDetailFragment newInstance(LiveRoom liveRoom) {
        LiveDetailFragment fg = new LiveDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("liveRoom",liveRoom);
        fg.setArguments(bundle);
        return fg;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_live_detail, null);
        unbinder = ButterKnife.bind(this, rootView);

        enterRoom.setOnClickListener(this);
        expertContainer.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void initData() {
        liveRoom = (LiveRoom)getArguments().getSerializable("liveRoom");
        presenter = new LiveDetailFragmentPresenter(this,getActivity());
        ImageLoader.getInstance().loadImageView(getActivity(),liveRoom.expertAvatar,expertAvatar);
        roomTitle.setText(liveRoom.title);
        summary.setText(liveRoom.summary);
        time.setText(liveRoom.getTimeString());
        enroled.setText("已报名"+liveRoom.enrolCount);
        allow.setText("/"+liveRoom.allowCount);
        liveId = liveRoom.roomId;
        price.setText(liveRoom.getPriceString());
        expertNameTitle.setText(liveRoom.expertName+" "+liveRoom.expertDepartment+" "+liveRoom.expertJobTitle);
        expertHospital.setText(liveRoom.expertHospital);
        expertSummary.setText(liveRoom.expertSummary);

//        if(liveRoom.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
//            enterRoom.setText("进入直播间");
//        }else{
        if(liveRoom.expertId.equals(InitManager.getInstance().getUserId())
                || liveRoom.serverId.equals(InitManager.getInstance().getUserId())){
            enterRoom.setText("进入直播间");
        } else {
            if(liveRoom.isEnroled == 0){
                enterRoom.setText("立即报名");
            }else{
                enterRoom.setText("进入直播间");
            }
        }
//        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.enter_room:
                if(InitManager.getInstance().getUser() == null){
                    LoginActivity.startForResult(getActivity());
                    return;
                }
                if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
                    BindMobileActivity.startForResult(getActivity());
                    return;
                }

                //3.1 added
//                if(liveRoom.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
//                    LiveRoomActivity.start(LiveDetailActivity.this,liveRoom);
//                    return;
//                }

                if(InitManager.getInstance().getUserId().equals(liveRoom.expertId)
                        || InitManager.getInstance().getUserId().equals(liveRoom.serverId)){
                    LiveRoomActivity.start(getActivity(),liveRoom);
                }else{
                    if(liveRoom.isEnroled == 0){
//                        showProgressDialog("请稍后...",false);
                        presenter.joinRoom(liveId+"");
                    }else{
                        LiveRoomActivity.start(getActivity(),liveRoom);
                    }
                }

                break;
            case R.id.expert_container:
                if(InitManager.getInstance().getUser() == null){
                    LoginActivity.startForResult(getActivity());
                    return;
                }
                if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
                    BindMobileActivity.startForResult(getActivity());
                    return;
                }
                ExpertProfileActivity.start(getActivity(), liveRoom.expertId);
                break;
        }
    }

    @Override
    public void onRequestFailed(String code, String reason) {

    }

    @Override
    public void onJoinSuccess() {
//        dismissProgressDialog();
        showToast("报名成功");
        LiveRoomActivity.start(getActivity(),liveRoom);
    }

    @Override
    public void onSharePointSuccess(String tips) {

    }
}
